package com.px.plan.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.px.common.utils.DateUtils;
import com.px.framework.mqtt.MqttServiceServiceImpl;
import com.px.framework.mqtt.model.TopicConst;
import com.px.plan.domain.PlanActionMark;
import com.px.plan.mapper.PlanActionMarkMapper;
import com.px.plan.service.IPlanActionMarkService;
import com.px.system.domain.vo.PushMqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 飞行行动标记Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class PlanActionMarkServiceImpl implements IPlanActionMarkService 
{
    @Autowired
    private PlanActionMarkMapper planActionMarkMapper;

    @Autowired
    private MqttServiceServiceImpl MqttService;

    /**
     * 查询飞行行动标记
     * 
     * @param id 飞行行动标记主键
     * @return 飞行行动标记
     */
    @Override
    public PlanActionMark selectPlanActionMarkById(Long id)
    {
        return planActionMarkMapper.selectPlanActionMarkById(id);
    }

    /**
     * 查询飞行行动标记列表
     * 
     * @param planActionMark 飞行行动标记
     * @return 飞行行动标记
     */
    @Override
    public List<PlanActionMark> selectPlanActionMarkList(PlanActionMark planActionMark)
    {
        return planActionMarkMapper.selectPlanActionMarkList(planActionMark);
    }

    /**
     * 新增飞行行动标记
     * 
     * @param planActionMark 飞行行动标记
     * @return 结果
     */
    @Override
    public int insertPlanActionMark(PlanActionMark planActionMark)
    {
        PushMqVo pushMqVo = new PushMqVo();
        pushMqVo.setPalnId(planActionMark.getActionId().toString());
        pushMqVo.setSn("");
        pushMqVo.setType(TopicConst.PLAN_ACTION_MARK);
        pushMqVo.setTimestamp(DateUtils.getNowDate().getTime());
        JSONObject data = new JSONObject();
        data.put("planActionId",planActionMark.getActionId());
        pushMqVo.setData(data);
        MqttService.pushPalnMq(pushMqVo);

        return planActionMarkMapper.insertPlanActionMark(planActionMark);
    }

    /**
     * 修改飞行行动标记
     * 
     * @param planActionMark 飞行行动标记
     * @return 结果
     */
    @Override
    public int updatePlanActionMark(PlanActionMark planActionMark)
    {
        PlanActionMark Pa = planActionMarkMapper.selectPlanActionMarkById(planActionMark.getId());
        PushMqVo pushMqVo = new PushMqVo();
        pushMqVo.setPalnId(Pa.getActionId().toString());
        pushMqVo.setSn("");
        pushMqVo.setType(TopicConst.PLAN_ACTION_MARK);
        pushMqVo.setTimestamp(DateUtils.getNowDate().getTime());
        JSONObject data = new JSONObject();
        data.put("planActionId",Pa.getActionId());
        pushMqVo.setData(data);
        MqttService.pushPalnMq(pushMqVo);
        return planActionMarkMapper.updatePlanActionMark(planActionMark);
    }

    /**
     * 批量删除飞行行动标记
     * 
     * @param ids 需要删除的飞行行动标记主键
     * @return 结果
     */
    @Override
    public int deletePlanActionMarkByIds(Long[] ids)
    {
        for(long id : ids){
            PlanActionMark Pa = planActionMarkMapper.selectPlanActionMarkById(id);
            PushMqVo pushMqVo = new PushMqVo();
            pushMqVo.setPalnId(Pa.getActionId().toString());
            pushMqVo.setSn("");
            pushMqVo.setType(TopicConst.PLAN_ACTION_MARK);
            pushMqVo.setTimestamp(DateUtils.getNowDate().getTime());
            JSONObject data = new JSONObject();
            data.put("planActionId",Pa.getActionId());
            pushMqVo.setData(data);
            MqttService.pushPalnMq(pushMqVo);
        }
        return planActionMarkMapper.deletePlanActionMarkByIds(ids);
    }

    /**
     * 删除飞行行动标记信息
     * 
     * @param id 飞行行动标记主键
     * @return 结果
     */
    @Override
    public int deletePlanActionMarkById(Long id)
    {
        return planActionMarkMapper.deletePlanActionMarkById(id);
    }
}
